
package com.ruoyi.gisconverter.core.type;

import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureSource;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


public class ShapeFile extends FileProcess {

    @Override
    public FeatureSource readFile(File file) {
        ShapefileDataStore ds = null;
        try {
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());

            ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
            ds = (ShapefileDataStore) factory.createDataStore(params);
            return ds.getFeatureSource();
        } catch (Exception e) {
            throw new RuntimeException("Error while reading Shape File!!!", e);
        } finally {
            if (ds != null) {
                ds.dispose();
            }
        }
    }

    @Override
    public void writeFile(FeatureSource fs, File file) {

        try {
            SimpleFeatureCollection collection = ff.getFeatureCollection(fs);

            ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put("url", file.toURI().toURL());
            params.put("create spatial index", Boolean.TRUE);

            ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
            dataStore.createSchema(collection.getSchema());
            dataStore.forceSchemaCRS(collection.getSchema().getCoordinateReferenceSystem());

            String typeName = dataStore.getTypeNames()[0];
            SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);

            if (featureSource instanceof SimpleFeatureStore) {
                SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

                Transaction transaction = new DefaultTransaction("create");
                featureStore.setTransaction(transaction);
                try {
                    featureStore.addFeatures(collection);
                    transaction.commit();

                } catch (Exception e) {
                    transaction.rollback();
                    throw e;
                } finally {
                    transaction.close();
                }
            } else {
                throw new RuntimeException("Can't write to new ShapeFileDatastore");
            }
        } catch (Exception ex) {
            throw new RuntimeException("Error while writing GeoJSON File!!!", ex);
        }
    }

}
